<!--
 * @Descripttion: 储值套餐
 * @Author: xiao li
 * @Date: 2020-09-03 11:44:42
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-18 13:55:53
-->
<template>
  <div>
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate flex-warp">
        <lb-button
          type="primary"
          @click="$router.push(`${appPath}/vip/editStored`)"
          icon="el-icon-plus"
          >{{ $t('menu.VipStoredAdd') }}</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="title" label="套餐名称"> </el-table-column>
        <el-table-column prop="pay_price" label="购买价格"> </el-table-column>
        <el-table-column prop="true_price" label="实际充值"> </el-table-column>
        <el-table-column prop="growth" label="赠送成长值"> </el-table-column>
        <el-table-column prop="top" label="排序"> </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toUpdateStatus(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="
                  $router.push(`${appPath}/vip/editStored?id=${scope.row.id}`)
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="toDel(scope.row.id, -1)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      searchForm: {
        page: 1,
        limit: 10
      },
      total: 0,
      tableData: []
    }
  },
  created () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
  },
  activated () {
    this.getTableDataList(1)
  },
  methods: {
    toResetForm () {
      this.$refs.multipleTable.clearSelection()
    },
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.memberStoreList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    toDel (id, status) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toUpdateStatus(id, status)
      })
    },
    async toUpdateStatus (id, status) {
      let { code } = await this.$api.memberStoreUpdate({ id, status })
      if (code === 200) {
        this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
        if (status === -1) {
          this.searchForm.page = this.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        }
      } else {
        if (status === -1) return
        this.getTableDataList()
      }
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select,
.el-input {
  width: 200px;
}
</style>
